{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a4bb1df-f558-481f-9232-aee402caa172",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Resume Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01137419-2b39-4819-962d-307c9b3f0ae9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 我是一名 HR\n",
    "# 日常需要很多时间去看简历来判断岗位的符合性\n",
    "# 再决定是否邀约进行面试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "414cd7c6-9ebd-4a87-90b4-179c5637ed48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pypdf in /opt/conda/lib/python3.10/site-packages (3.9.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install pypdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e1cf433a-dce3-4628-9046-39688836d075",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import openai, os\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.llms import OpenAIChat\n",
    "from langchain.chains import LLMChain \n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.document_loaders import PyPDFLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "097b8c50-0bf5-4756-a1fd-4a239a9d882a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import Config\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"]=Config.OPENAI_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a0797aae-5e33-4fa6-855c-be9fe574d0cf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.10/site-packages/langchain/llms/openai.py:696: UserWarning: You are trying to use a chat model. This way of initializing it is no longer supported. Instead, please use: `from langchain.chat_models import ChatOpenAI`\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "llm = OpenAIChat(model_name=\"gpt-3.5-turbo\", max_tokens=2048, temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebd08cd2-aa3c-4f81-97c6-23e157a9c302",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a1b775ff-91cb-42f5-a516-a4c969c17f79",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "loader = PyPDFLoader(\"./datasets/1.pdf\")\n",
    "pages = loader.load_and_split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4f70a870-cad2-4f99-8aaf-21a3e0a408df",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "墨东东\n",
      "男｜45岁｜广州-黄埔区｜13年工作经验\n",
      "联系方式：186-123-4567modongdong@gmail.com\n",
      "最近工作（2年11个月）\n",
      "大数据开发｜海克斯高科技有限公司\n",
      "教育信息\n",
      "计算机｜清华大学｜大专1990.09-1993.06\n",
      "计算机｜北京大学｜本科1993.09-1997.06\n",
      "数仓工程师8000-12000/月\n",
      "广州｜东莞｜全职\n",
      "实施工程师8000-12000/月\n",
      "广州｜东莞｜全职\n",
      "大数据开发8000-12000/月\n",
      "广州｜东莞｜全职\n",
      "Hadoop生态体系：\n",
      "1.掌握Hadoop生态相关集群,包括HDFS,YARN,Hive等,并完成Hadoop各组件调优；\n",
      "2.掌握HDFS的架构,读写流程；？\n",
      "3.掌握YARN的架构,运行原理,能与不同的计算框架结合使用；\n",
      "4.掌握MapReduce的计算原理,尤其shuffle过程,能使用Java开发MR程序；\n",
      "5.掌握Hive的架构,运行原理,能写HQL语句完成数据分\n",
      "Spark生态体系：\n",
      "1.熟悉Spark生态集群,理解各个组件的内在关联；2.掌握RDD算子原理,能使用SparkCore开发；\n",
      "3.掌握结构化数据计算模型,能使用DataFrame/DataSet组件开发；\n",
      "1.掌握流式计算模型,并能使用SparkStreaming进行流式程序开发；\n",
      "2.掌握Spark计算模型原理,GraphX图计算编程，能实现Spark调优以及数据倾斜\n",
      "其他组件\n",
      "1.掌握Flume,架构,运行原理,并能编写配置文件,实现源数据的收集；\n",
      "2.掌握Kafka的架构,运行原理,并能解决生产者与消费者之间的消费关系；\n",
      "3.掌握Java/Scala编程；\n",
      "4.掌握MySQL数据库基本使用,完成项目中需要的sql语句编写\n"
     ]
    }
   ],
   "source": [
    "print(pages[0].page_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15c3075e-927b-438c-85cc-1009b89647f5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "## 数据预处理，清理水印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "707b13f3-4646-4361-ba16-6c50b2392502",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "clear_template = \"\"\"\n",
    "请针对 >>> 和 <<< 中间的简历信息，消除水印信息，水印格式大概是这样的格式:'f3fp ID：377000 2023-05-30 XXX科技有限公司'，如果给定的内容都是水印信息则输出空。\n",
    "\n",
    ">>> {question} <<<\n",
    "\"\"\"\n",
    "\n",
    "clear_prompt = PromptTemplate(template=clear_template, input_variables=[\"question\"])\n",
    "\n",
    "clear_chain = LLMChain(llm=llm, prompt=clear_prompt, output_key=\"answer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67a648e4-3406-44eb-9c3c-efcb17291658",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "print(clear_chain.run(question=pages[0].page_content))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fdff6a1-b59d-46c2-a153-4e507d79eca7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.text_splitter import SpacyTextSplitter\n",
    "text_splitter = SpacyTextSplitter(chunk_size=2048, pipeline=\"zh_core_web_sm\")\n",
    "texts = text_splitter.split_documents(pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eca51525-3c9f-43d4-93c0-124f5bf403f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "clear_texts = []\n",
    "\n",
    "for text in texts:\n",
    "    clear_text = clear_chain.run(question=text.page_content)\n",
    "    clear_texts.append(clear_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66f7d52a-5188-4613-9ad8-577c6b362229",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "## 抽取基础信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e3e15a74-6a26-4c54-a0a7-157a377f03c9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "base_template = \"\"\"\n",
    "请针对 >>> 和 <<< 中间的简历信息，抽取出模版中字段对应的值，如果不存在则输出 None，按照指定的模版格式输出JSON。\n",
    ">>> {context} <<<\n",
    "\n",
    "###\n",
    "  \"姓名\": \"\",\n",
    "  \"年龄\": \"\",\n",
    "  \"所在地区\": \"\",\n",
    "  \"工作年限\": \"\", # 最小颗粒度可以是月\n",
    "  \"手机号码\": \"\",\n",
    "  \"邮箱地址\": \"\",\n",
    "  \"教育信息\": [\"学校\": \"\", \"专业\": \"\", \"学历\": \"\", \"开始时间\": \"\", \"结束信息\": \"\"], # 学历信息可以有多个\n",
    "  \"求职意向\": [\"岗位\": \"\", \"地区\": \"\", \"薪酬\": \"\"], # 求职意向可以有多个\n",
    "###\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "base_prompt = PromptTemplate(template=base_template, input_variables=[\"context\"])\n",
    "\n",
    "base_chain = LLMChain(llm=llm, prompt=base_prompt, output_key=\"output\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8e3b99c1-178c-4117-8029-6a69b1da0743",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"姓名\": \"墨东东\",\n",
      "  \"年龄\": \"45岁\",\n",
      "  \"所在地区\": \"广州-黄埔区\",\n",
      "  \"工作年限\": \"13年\",\n",
      "  \"手机号码\": \"186-123-4567\",\n",
      "  \"邮箱地址\": \"modongdong@gmail.com\",\n",
      "  \"教育信息\": [\n",
      "    {\n",
      "      \"学校\": \"清华大学\",\n",
      "      \"专业\": \"计算机\",\n",
      "      \"学历\": \"大专\",\n",
      "      \"开始时间\": \"1990.09\",\n",
      "      \"结束信息\": \"1993.06\"\n",
      "    },\n",
      "    {\n",
      "      \"学校\": \"北京大学\",\n",
      "      \"专业\": \"计算机\",\n",
      "      \"学历\": \"本科\",\n",
      "      \"开始时间\": \"1993.09\",\n",
      "      \"结束信息\": \"1997.06\"\n",
      "    }\n",
      "  ],\n",
      "  \"求职意向\": [\n",
      "    {\n",
      "      \"岗位\": \"数仓工程师\",\n",
      "      \"地区\": \"广州｜东莞\",\n",
      "      \"薪酬\": \"8000-12000/月\"\n",
      "    },\n",
      "    {\n",
      "      \"岗位\": \"实施工程师\",\n",
      "      \"地区\": \"广州｜东莞\",\n",
      "      \"薪酬\": \"8000-12000/月\"\n",
      "    },\n",
      "    {\n",
      "      \"岗位\": \"大数据开发\",\n",
      "      \"地区\": \"广州｜东莞\",\n",
      "      \"薪酬\": \"8000-12000/月\"\n",
      "    }\n",
      "  ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "result_base = base_chain.run(context=pages[0].page_content)\n",
    "print(result_base)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d93f0493-867a-4b2d-9fdd-58bf3d95bead",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "project_template = \"\"\"\n",
    "请针对 >>> 和 <<< 中间的简历信息，抽取出模版中字段对应的值，如果不存在则输出 None，按照指定的模版格式输出JSON。\n",
    ">>> {question} <<<\n",
    "\n",
    "###\n",
    "  \"工作经历\": [\"公司名称\": \"\", \"担任岗位\": \"\", \"开始时间\": \"\", \"结束时间\": \"\"] # 工作经历可以有多个\n",
    "  \"项目经验\": [\"项目名称\": \"\", \"所在公司\": \"\", \"项目描述\": \"\", \"个人职责\": \"\", \"所用技术\": [\"技术名\"]] # 项目经验可以有多个，技术栈可以有多个\n",
    "###\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "project_prompt = PromptTemplate(template=project_template, input_variables=[\"question\"])\n",
    "\n",
    "project_chain = LLMChain(llm=llm, prompt=project_prompt, output_key=\"answer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "858a66f7-37a0-4f83-90b0-c5b1536f829a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"工作经历\": [\n",
      "    {\n",
      "      \"公司名称\": \"海克斯高科技有限公司\",\n",
      "      \"担任岗位\": \"大数据开发\",\n",
      "      \"开始时间\": \"2020.06\",\n",
      "      \"结束时间\": \"至今（2年11个月）\"\n",
      "    }\n",
      "  ],\n",
      "  \"项目经验\": [\n",
      "    {\n",
      "      \"项目名称\": \"海克斯用户画像\",\n",
      "      \"所在公司\": \"海克斯高科技有限公司\",\n",
      "      \"项目描述\": \"本次项目主要是想通过对达丰电脑销售网站web端的用户信息进行收集，做用户画像，对用户进行打标签，针对高质量用户，精准的推送广告，增加用户粘性，提升用户留存，让他们能通过广告的推送,提升活跃度，刺激消费。在数据应用上，可以用来推荐、搜索、风控，将定性分析和定量分析结合，进行数据化运营和用户分析，分析产品的潜在用户和用户的潜在需求，针对特定群体，利用短信、邮件等方式进行营销，对产品进行受众分析，更透彻地理解用户使用产品的心理动机和行为习惯，完善产品运营，提升服务质量。还可以通过用户画像分析了解行业动态，比如人群消费习惯、消费偏好分析、不同地域品类消费差异分析。\",\n",
      "      \"个人职责\": \"主要负责Spark编码实现部分\",\n",
      "      \"所用技术\": [\"SparkCore\", \"SparkSQL\"]\n",
      "    },\n",
      "    {\n",
      "      \"项目名称\": \"海克斯数据云\",\n",
      "      \"所在公司\": \"海克斯高科技有限公司\",\n",
      "      \"项目描述\": \"该项目是以数据云平台开发业务，目标是建设一套标准的数据体系，在业务使用数据时统一指标口径，解决每次临时补充数据，提升数据供给的效率。本项目严格做到指标间同名同义、异名异议一致性保障和同指标与明细数据之间的数据一致性；数据仓库稳定运行，每日支撑约60+城市，1000人，上万次的ADhoc查询；通过数据模型支持以及多个数据应用场景的产品化，解决90%以上的数据需求。\",\n",
      "      \"个人职责\": \"整体负责从项目需求沟通、方案规划、人员分工和项目落地。\",\n",
      "      \"所用技术\": [\"sqoop\", \"Flink\", \"Canal\", \"HiveSQL\"]\n",
      "    }\n",
      "  ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "result_project = project_chain.run(question=pages[1].page_content)\n",
    "print(result_project)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49129508-f09c-4be8-9c22-30912a48ef97",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "score_template = \"\"\"\n",
    "你是资深的计算机科学面试官，请针对 *** 中间的岗位描述，以及 >>> 和 <<< 中间的简历信息，进行相关度评分(不相关的0分，是越相似的10分)，如果不存在则输出 None，按照指定的模版格式输出JSON。\n",
    ">>> {jd} <<<\n",
    "*** {cv} ***\n",
    "\n",
    "###\n",
    "  \"所在地区\": \"\" # 接近广东珠三角地区的得分越高\n",
    "  \"工作年限\": \"\" \n",
    "  \"最高学历\": \"\" \n",
    "  \"相关专业\": \"\" \n",
    "  \"项目经验\": \"\" # 根据项目的关键字进行相关性搜索\n",
    "  \"技术匹配度\": \"\" # 根据所使用的工具进行相关联搜索\n",
    "  \"是否邀约\": Bool # 总结\n",
    "  \"最终总结\": \"\" # 岗位描述和简历信息的匹配度总结，请用50个字内描述\n",
    "  \n",
    "###\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "score_prompt = PromptTemplate(template=score_template, input_variables=[\"jd\", \"cv\"])\n",
    "\n",
    "score_chain = LLMChain(llm=llm, prompt=score_prompt, output_key=\"score\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ae7c1673-d300-4366-b848-e00114c08f5b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# cv - curriculum vitae \n",
    "resume_all = result_base + \"\\n\" + result_project"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "91b2fc2c-afa7-4572-ba1c-fc0a848cf807",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jd - job description\n",
    "jd_all = '岗位职责：\\\n",
    "1.负责公司楼盘的现场推介及促进客户成交；\\\n",
    "2.熟悉新房交易全流程工作，收集并及时处理客户反馈意见；\\\n",
    "3.定期对销售数据及成交客户资料进行分析评估。\\\n",
    "任职要求：\\\n",
    "1.大专以上学历优先；\\\n",
    "2.形象气质佳，沟通表达能力强，熟练掌握房地产销售技能，具有较强应变能力;\\\n",
    "3.有住宅/别墅新房销售经验及销拓一体经验优先；\\\n",
    "4.会讲粤语。'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47c1563d-5863-43d4-b107-f1dba05f6833",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"所在地区\": 8,\n",
      "  \"工作年限\": 6,\n",
      "  \"最高学历\": 2,\n",
      "  \"相关专业\": 0,\n",
      "  \"项目经验\": 7,\n",
      "  \"技术匹配度\": 8,\n",
      "  \"是否邀约\": false,\n",
      "  \"最终总结\": \"简历中的工作经验和项目经验与岗位描述较为匹配，但学历和专业不太符合要求。建议面试时重点考察其实际工作能力和销售技能。\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "score = score_chain.run(jd=jd_all, cv=resume_all)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8da46a87-dc50-4943-a13c-6544aabcad8e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "jd_all = '职位详情 \\\n",
    "1、负责大数据平台建设，包括数据采集，清洗，存储，分析，可视化 等 \\\n",
    "对接各类业务，采集各来源数据至数仓；进行数据沉淀\\库表设计\\ETL开发及性能优化 \\\n",
    "2、进行数仓结构设计及实现，数据治理工作，提升数据易用性及数据质量 \\\n",
    "建立实时和离线大数据处理流程，负责数据产品平台化和系统化 \\\n",
    "职位要求 \\\n",
    "1、计算机及相关专业研究生以上学历 \\\n",
    "2、从事数据仓库或大数据领域相关开发2年及以上 \\\n",
    "3、熟悉HDFS/Hive/Hbase/Spark/Flink/Kylin/Clickhouse等一种或多种相关技术 \\\n",
    "4、熟悉Scala、Python、SQL/PLSQL开发中的一种或多种，有良好编程习惯 \\\n",
    "5、有数据采集、大数据处理、数据仓库建模、ETL系统设计等相关开发或优化经验'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "118693c7-3def-48df-b8da-63c079ce00c4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"所在地区\": 8,\n",
      "  \"工作年限\": 10,\n",
      "  \"最高学历\": 5,\n",
      "  \"相关专业\": 10,\n",
      "  \"项目经验\": 8,\n",
      "  \"技术匹配度\": 9,\n",
      "  \"是否邀约\": true,\n",
      "  \"最终总结\": \"该候选人在大数据领域有丰富的经验，熟悉多种相关技术和工具，项目经验丰富，能够胜任本岗位工作。\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "score = score_chain.run(jd=jd_all, cv=resume_all)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01e749d5-354b-4153-b068-788a7b96a1ed",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe0a0ecf-fd97-4003-bc36-3177eb2cc784",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cf626e9-2e4b-4400-9e90-0e5effb16f11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6944b46-9ed8-4940-ae4c-1558aa627ffa",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "426979e8-5759-456b-86e2-ca8066e34bef",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "703327e8-edf3-44c8-b3c0-a260eefbde5d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13c8ebc3-00d9-418d-b71d-f619e969b1f5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbd00e51-5d43-402a-b9ba-7a14e3bfdd37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "986b4aa3-f093-4404-9e12-5804aebdf58e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0cd8277-fdbf-4db5-8099-7d93dbd37c35",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
